package com.dtkavin.zk;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

/**
 * Created by IntelliJ IDEA.
 * Programmer : John Zn
 * Date : 2016/4/21 0021
 * Time : 13:15
 * Discribtion : zk上创建节点
 */
public class TestServer {
    private String groupNode = "sgroup";
    private String subNode = "sub";

    public void connectZK(String address) throws Exception {

        ZooKeeper zk = new ZooKeeper("192.168.98.72:2181", 5000, new Watcher() {

            //            @Override
            public void process(WatchedEvent event) {
                // TODO Auto-generated method stub

            }
        });
        //createNode  ACL 访问控制列表
        String path = zk.create("/sgroup/sub", address.getBytes("utf-8"), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println("created: " + path);


    }

    public void handle() throws InterruptedException {

        Thread.sleep(Long.MAX_VALUE);
    }

    public static void main(String[] args) throws Exception {
        if (args.length == 0) {
            System.err.println("wrong params...");
            System.exit(1);
        }
        TestServer server = new TestServer();
        server.connectZK(args[0]);
        server.handle();
    }
}
